package com.example.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.common.Result;
import com.example.domain.SeckillVoucher;
import com.example.domain.Voucher;
import com.example.mapper.VoucherMapper;
import com.example.service.SeckillVoucherService;
import com.example.service.VoucherService;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

import static com.example.common.RedisContants.SECKILL_STOCK_KEY;

/**
 * @author chenliang
 * @description 针对表【tb_voucher】的数据库操作Service实现
 * @createDate 2024-07-02 20:20:03
 */
@Service
public class VoucherServiceImpl extends ServiceImpl<VoucherMapper, Voucher>
        implements VoucherService {

    @Resource
    private SeckillVoucherService seckillVoucherService;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 新增秒杀券
     *
     * @param voucher
     */
    @Override
    @Transactional
    public void addSeckillVoucher(Voucher voucher) {
        // 保存优惠券
        save(voucher);
        // 保存秒杀信息
        SeckillVoucher seckillVoucher = new SeckillVoucher();
        seckillVoucher.setVoucherId(voucher.getId());
        seckillVoucher.setStock(voucher.getStock());
        seckillVoucher.setBeginTime(voucher.getBeginTime());
        seckillVoucher.setEndTime(voucher.getEndTime());
        seckillVoucherService.save(seckillVoucher);
        // 保存秒杀库存到Redis中
        String stockKey = SECKILL_STOCK_KEY + voucher.getId();
        stringRedisTemplate.opsForValue().set(stockKey, voucher.getStock().toString());
    }


    @Override
    public Result queryVoucherOfShop(Long shopId) {
        return null;
    }
}




